<template>
<el-row :gutter="20" style="padding: 20px 10px; margin: 0px">
	<el-form ref="form" label-width="80px" size="mini" label-position="left">
			<el-col :span="12">
				<el-form-item label="产品编号">
					<el-input :value="goods.number" disabled></el-input>
				</el-form-item>
				<el-form-item label="产品名称">
					<el-input :value="goods.name" disabled></el-input>
				</el-form-item>
				<el-form-item label="产品价格">
					<el-input :value="goods.price" disabled></el-input>
				</el-form-item>
				<el-form-item>
				    <el-button type="primary" @click="submit">保存</el-button>
				    <el-button @click="close">取消</el-button>
				 </el-form-item>

			</el-col>
			<el-col :span="12">
				<el-form-item label="销售类型">
					<el-radio-group v-model="goods.tdtype">
						<el-radio :label="1">百分比</el-radio>
						<el-radio :label="2">固定金额</el-radio>
					</el-radio-group>
					<el-input v-model="goods.percent" placeholder="请输入销售百分比提成(0 ~ 100)" v-if="goods.tdtype==1"></el-input>
					<el-input v-model="goods.fixed" placeholder="请输入销售固定金额提成" v-else></el-input>
				</el-form-item>
				<el-form-item label="耗卡类型">
					<el-radio-group v-model="goods.khtdtype">
						<el-radio :label="1">百分比</el-radio>
						<el-radio :label="2">固定金额</el-radio>
					</el-radio-group>
					<el-input v-model="goods.khpercent" placeholder="请输入耗卡百分比提成(0 ~ 100)" v-if="goods.khtdtype==1"></el-input>
					<el-input v-model="goods.khfixed" placeholder="请输入耗卡固定金额提成" v-else></el-input>
				</el-form-item>


			</el-col>
		</el-form>
</el-row>
</template>
<script>
import C from '#/src/common/common.js'
import Goods from '#/src/shop/goods.js'
export default {
    name: 'editGoods',
    data () {
        return {
            goods: {}
        }
    },
    methods: {
        async submit () {
            if (!/^[1-9]\d*(\.\d+)?$/.test(this.goods.price)) {
                C.error('请正确输入产品价格')
                return false
            }

            if (this.goods.tdtype != 1 && this.goods.tdtype != 2) {
                C.error('请选择提成类型')
                return false
            }

            if (this.goods.tdtype==1) {
                this.goods.fixed = 0
                if (!/^(\d|[1-9]\d|100)$/.test(this.goods.percent)) {
                    C.error('请输入0 ~ 100的百分比提成')
                    return false
                }
            } else {
                this.goods.percent = 0
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.goods.fixed)) {
                    C.error('请输入固定金额提成')
                    return false
                }
            }

            if (this.goods.khtdtype==1) {
                this.goods.khfixed = 0
                if (!/^(\d|[1-9]\d|100)$/.test(this.goods.khpercent)) {
                    C.error('请输入0 ~ 100的百分比耗卡提成')
                    return false
                }
            } else {
                this.goods.khpercent = 0
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.goods.khfixed)) {
                    C.error('请输入固定金额耗卡提成')
                    return false
                }
            }
            var ssum = 0
            var khsum = 0
            if(this.goods.tdtype === 1){
                ssum = parseInt(this.goods.price)*parseInt(this.goods.percent)/100
            }else{
                ssum = parseInt(this.goods.fixed)
            }

            if(this.goods.khtdtype === 1){
                khsum = parseInt(this.goods.price)*parseInt(this.goods.khpercent)/100
            }else{
                khsum = parseInt(this.goods.khfixed)
            }
            var allsum = parseInt(ssum+khsum)
            if(this.goods.price < allsum){
                C.error('输入的提成超出产品价格')
                return false
            }
            let res = await Goods.editGoods(this.goods)
            C.closeBrowserWindow(res)
        },
        close () {
            C.closeBrowserWindow()
        }
    },
    mounted: function(){
        let row = C.getDatas()
        row.percent = Number(row.percent*100).toFixed(0)
        row.khpercent = Number(row.khpercent*100).toFixed(0)
        this.goods = row
    }
}
</script>